/**
 * Created by zhoua on 2017/3/27.
 */
/* eslint-disable */
function GetDate (date) {
    let time = date ? new Date(date) : new Date()
    this.year = time.getFullYear() + ''
    let month = time.getMonth() + 1
    this.month = month  >= 10 ? month : '0' + month + ''
    this.day = time.getDate() >= 10 ? time.getDate() : '0' + time.getDate() + ''
    this.hours = time.getHours() >= 10 ? time.getHours() : '0' + time.getHours() + ''
    this.min = time.getMinutes() >= 10 ? time.getMinutes() : '0' + time.getMinutes() + ''
    this.sec = time.getSeconds() >= 10 ? time.getSeconds() : '0' + time.getSeconds() + ''
    this.transformDate = function (date, isTime, separators) {
      separators = separators ? separators : '-'
      if (date === null || date === undefined) return ''
      if (date === '') return ''
      let time = date ? new Date(date) : new Date()
      let year = time.getFullYear() + ''
      let month = time.getMonth() + 1 >= 10 ? time.getMonth() : '0' + time.getMonth() + ''
      let day = time.getDate() >= 10 ? time.getDate() : '0' + time.getDate() + ''
      let hours = time.getHours() >= 10 ? time.getHours() : '0' + time.getHours() + ''
      let min = time.getMinutes() >= 10 ? time.getMinutes() : '0' + time.getMinutes() + ''
      let sec = time.getSeconds() >= 10 ? time.getSeconds() : '0' + time.getSeconds() + ''
      month = +month + 1 >= 10 ? month : '0' + (++month)
      time = year + separators + month + separators + day
      if (isTime) {
        tiem += ' ' + hours + ':' + min + ':' + sec
      }
      return time
    }
}
GetDate.prototype.lastMonth = function () {
  var lastMonth = this.month
  return this.year + '-' + (this.month) + '-' + this.day
}
GetDate.prototype.latest30Day = function () {
  return 'abc'
}
GetDate.prototype.lastYear = function () {
  return --this.year + '-' + this.month
}
GetDate.prototype.getDayForMonth = function (num, after) {
  num = num || 7
  if (after) {
    var afterNum = num + 1
  } else {
    afterNum = num
  }
  var startDate = Date.now() - (1000 * 3600 * 24 * afterNum)
  startDate = new GetDate(startDate)
  var endDate
  if (after) {
    endDate = new GetDate(Date.now() - 3600 * 1000 * 24)
  } else {
    endDate = new GetDate()
  }
  console.log(num, startDate, endDate)
  var dayArr = []
  if (+num > 31) {
    for (let i = +startDate.month; i <= 12; i++) {
      let anyMonth = i >= 10 ? '' + i : '0' + i
      dayArr.push(startDate.year + '-' + anyMonth)
    }
    for (let j = 1; j < +endDate.month; j++) {
      let anyMonth = j >= 10 ? '' + j : '0' + j
      dayArr.push(endDate.year + '-' + anyMonth)
    }
    return dayArr
  }
  if (['01', '03', '05', '07', '08', '10', '12'].indexOf(startDate.month) !== -1) {
    var monthLength = 31
  } else if (startDate.month === '02') {
    if (!(startDate.year % (startDate.year % 100 ? 4 : 400))) {
      monthLength = 28
    } else {
      monthLength = 29
    }
  } else {
    monthLength = 30
  }
  if (+num <= 7) {
    if (startDate.month === endDate.month) {
      for (var z = +startDate.day + 1; z <= +endDate.day; z++) {
        var endDay1 = z
        if (z < 10) {
          endDay1 = '0' + endDay1
        } else {
          endDay1 = endDay1 + ''
        }
        dayArr.push(endDate.month + '-' + endDay1)
      }
    } else {
      for (var i = +startDate.day; i <= monthLength; i++) {
        var startDay = i
        if (i < 10) {
          startDay = '0' + startDay
        } else {
          startDay = startDay + ''
        }
        dayArr.push(startDate.month + '-' + startDay)
      }
      for (var j = 1; j <= +endDate.day; j++) {
        var endDay = j
        if (j < 10) {
          endDay = '0' + endDay
        } else {
          endDay = endDay + ''
        }
        dayArr.push(endDate.month + '-' + endDay)
      }
    }
  } else {
    if (startDate.month === endDate.month) {
      for (var j = 1; j <= +endDate.day; j++) {
        var endDay = j
        if (j < 10) {
          endDay = '0' + endDay
        } else {
          endDay = endDay + ''
        }
        dayArr.push(endDate.month + '-' + endDay)
      }
    } else {
      for (var i = +startDate.day; i <= monthLength; i++) {
        var startDay = i
        if (i < 10) {
          startDay = '0' + startDay
        } else {
          startDay = startDay + ''
        }
        dayArr.push(startDate.month + '-' + startDay)
      }
      for (var j = 1; j <= +endDate.day; j++) {
        var endDay = j
        if (j < 10) {
          endDay = '0' + endDay
        } else {
          endDay = endDay + ''
        }
        dayArr.push(endDate.month + '-' + endDay)
      }
    }
  }
  return dayArr
}




export default {
  $Date: GetDate,
  getDate (date) {
    let time = date ? new Date(date) : new Date()
    let year = time.getFullYear() + ''
    let month = time.getMonth() + 1 >= 10 ? time.getMonth() : '0' + time.getMonth() + ''
    let day = time.getDate() >= 10 ? time.getDate() : '0' + time.getDate() + ''
    let hours = time.getHours() >= 10 ? time.getHours() : '0' + time.getHours() + ''
    let min = time.getMinutes() >= 10 ? time.getMinutes() : '0' + time.getMinutes() + ''
    let sec = time.getSeconds() >= 10 ? time.getSeconds() : '0' + time.getSeconds() + ''
    let createTime = ''
    if (month > 10) {
      createTime = year + '-' + (++month) + '-' + day + ' ' + hours + ':' + min + ':' + sec
    } else {
      createTime = year + '-' + ('0' + ++month) + '-' + day + ' ' + hours + ':' + min + ':' + sec
    }
    return createTime
  },

  getMonth (date) {
    var data = new GetDate();
    if (+data.month > 10) {
      return data.year + '-' + (data.month)
    } else {
      return data.year + '-' + (data.month)
    }
  },
  deleteId (data) {
    if (!data.children) {
      return
    } else {
      delete data.id
      for (var i = 0; i < data.children.length; i++) {
        this.deleteId((data.children)[i])
      }
    }
  },
  transformDate (date, separators) {
    separators = separators ? separators : '-'
    if (date === null || date === undefined) return ''
    if (date === '') return ''
    let time = date ? new Date(date) : new Date()
    let year = time.getFullYear() + ''
    let month = time.getMonth() + 1 >= 10 ? time.getMonth() : '0' + time.getMonth() + ''
    let day = time.getDate() >= 10 ? time.getDate() : '0' + time.getDate() + ''
    month = +month + 1 >= 10 ? month : '0' + (++month)
    time = year + separators + month + separators + day
    return time
    // return date.slice(0, date.indexOf('T'))
  },
  getRoot (resources, sideData, headData) {
    resources.forEach((item, index) => {
      headData.forEach((item3, index3) => {
        if (item.id === item3.id) {
          item3.isShow = true
        }
      })
    })
    resources.forEach((itemRes, indexRes) => {
      sideData.forEach((item2, index2) => {
        if (itemRes.id === item2.id) {
          item2.isShow = true
        }
      })
    })
  },
  /**
   * 深复制对象方法
   * @param obj A Object
   */
  copyObject (obj) {
    var cloneObj = function (obj) {
      var newObj = {}
      if (obj instanceof Array) {
        newObj = []
      }
      for (var key in obj) {
        var val = obj[key]
        // newObj[key] = typeof val === 'object' ? arguments.callee(val) : val; //arguments.callee 在哪一个函数中运行，它就代表哪个函数, 一般用在匿名函数中。
        newObj[key] = typeof val === 'object' ? cloneObj(val) : val
      }
      return newObj
    }
    return cloneObj(obj)
  },
  unique (arr) {
    var res = [];
    var json = {};
    for(var i = 0; i < arr.length; i++){
      if(!json[arr[i]]){
        res.push(arr[i])
        json[arr[i]] = 1
      }
    }
    return res;
  },
  getFileSize (obj, size) {
    var fileSize = 0;
    var isIE = /msie/i.test(navigator.userAgent) && !window.opera;
    if (isIE && !obj) {
      var filePath = obj.value;
      var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
      var file = fileSystem.getFile (filePath);
      fileSize = file.size;
    }else {
      fileSize = obj.size;
    }
    fileSize = Math.round(fileSize / 1024 * 100) / 100;
    if (fileSize >= size) {
      return ''
    }
    return
  },
  formatDate (date) {
    if (!date) {
      return ''
    }
    let time = new Date(date)
    let year = time.getFullYear() + ''
    let month = time.getMonth() + 1 >= 10 ? time.getMonth() : '0' + time.getMonth() + ''
    let day = time.getDate() >= 10 ? time.getDate() : '0' + time.getDate() + ''
    let createDate = ''
    if (month >= 10) {
      createDate = year + '-' + (++month) + '-' + day
    } else {
      createDate = year + '-' + '0' + (++month) + '-' + day
    }
    return createDate
  },
  formatTime (time) {
    if (!time || time == '') {
      return ''
    }
    var arr = time.replace(/ |:/g, '-').split('-')
    for (var i = 0; i < arr.length; i++) {
      arr[i] = parseInt(arr[i])
    }
    var date = new Date(Date.UTC(arr[0], arr[1] - 1, arr[2], arr[3] - 8, arr[4], arr[5]))
    var time3 = Date.parse(date)
    return date
  },
  downloadFile (file, origin) {

    var aLink = document.createElement('a')
    aLink.download = file.name.substring(1, file.name.length)
    if (origin) {
      aLink.href = origin + file.path + '/' + file.name
    } else {
      aLink.href = document.location.origin + file.path + '/' + file.name
    }
    aLink.click()
  },
  ajaxPost (that, reqObj) {
    that.$http.post(reqObj.url, reqObj.data)
      .then(res => {
        if (res.status >= 200 && res.status < 300) {
          if (res.data.result_code === 0) {
            reqObj.successCB(res)
          } else {
            if (res.data.result_code) {
              that.$message({
                message: res.data.result_detail,
                type: 'error',
                showClose: true
              })
            } else {
              that.$message({
                message: '服务器接口错误',
                type: 'error',
                showClose: true
              })
            }
          }
        } else {
          that.$message({
            message: '服务器错误',
            type: 'error',
            showClose: true
          })
        }
      })
      .catch(err => {
        if(err.response) {
          console.log(err.response.data);
          console.log(err.response.status);
          console.log(err.response.headers);
        } else {
          console.log('Error', err.message)
        }
        console.log(err.config)
      })
  },
  successCB (that, res, todo) {
    if (res.data.result_code === 0) {
        todo(res)
    } else {
      if (res.data.result_code) {
        that.$message({
          message: res.data.result_detail,
          type: 'error',
          showClose: true
        })
      } else {
        that.$message({
          message: '服务器接口错误',
          type: 'error',
          showClose: true
        })
      }
    }
  }
}
